<script setup lang="ts">
import { ref } from "vue";
import RealTimeVideo from "@/views/dashboard/right/video/realTimeVideo.vue";
import { useDeviceListApi } from "@/api/gps/device";
import { cloneDeep } from "lodash";
import { useUserStore } from "@/store/modules/user";
import { jt1078AvsetAnalysis } from "@/utils/gps/format";
import { Camera } from "@/interface/video";
import useDeviceStore from "@/store/gps/device";

const realTimeVideoRef = ref()
const userStore = useUserStore()
const deviceStore = useDeviceStore()
const followDeviceList = ref([])
const cameraList = ref<Camera[]>([])
let index = 0

console.log(userStore.user);
useDeviceListApi(cloneDeep({
  deptId: deviceStore.curSelectOrgId || userStore.user.orgId,
  connectionStatus: 'favor',
  orderField: 'signal_update_time',
  onlyVideo: false,
  order: 'DESC',
  keyWords: '',
  pageNum: 1,
  pageSize: 1000
})).then((res) => {
  followDeviceList.value = res.data.list
  InitializeChildNodesForNodes()
  console.log(cameraList.value);
  loopPlayback()
})

/**
 * 初始化设备的子节点
 * @constructor
 */
function InitializeChildNodesForNodes() {
  followDeviceList.value.forEach((item: any) => {
    const channelNames = jt1078AvsetAnalysis(item.jt1078Avch, item.jt1078Avset)
    if (channelNames.length === 0) {
      return
    }
    for (let i = 0; i < channelNames.length; i++) {
      let cl = channelNames[i]
      cameraList.value.push({
        plateNoName: item.plateNo,
        plateNo: cl.name,
        parentImei: item.imei,
        channel: cl.channel,
        imei: `${item.imei}-${i + 1}`,
        type: 'camera'
      })
    }
  })
}

function loopPlayback(){
    console.log("循环播放",index);
   setTimeout(()=>{
     realTimeVideoRef.value.insertVideo(cameraList.value[index])
     index++
     if(index > cameraList.value.length - 1 && index <= 36) return
     loopPlayback()
   }, 2000)
}
</script>

<template>
  <RealTimeVideo ref="realTimeVideoRef" fullScreen :cellCount="9"></RealTimeVideo>
</template>
